package com.itheima.reggie.controller;


import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author 
 * @since 2024-05-25
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;

    @PostMapping("/login")
    public R<User> login(@RequestBody User user, HttpSession session){
        log.info("传入的请求手机号:{}",user.getPhone());
        User userInfo = userService.lambdaQuery().eq(User::getPhone, user.getPhone()).one();
        //如果该用户不存在,则自动进行注册
        if(userInfo==null){
            User user1 = new User();
            user1.setPhone(user.getPhone());
            user1.setStatus(1);
            userService.save(user1);
            //将回显的id存入session中
            Long id = user1.getId();
            session.setAttribute("user",id);
            return R.success(user1);
        }
        Long id = userInfo.getId();
        session.setAttribute("user",id);
        return R.success(userInfo);
    }
}
